#include "tools.h"

//获取指令
int get_cmd(char start,char end)
{
    
	puts("******************");
	printf("请输入指令:");
	while(1)
	{
		int cmd =getch();
		if(start <= cmd && cmd <= end)
		{
			printf("%c\n",cmd);
			return cmd;
		}
	}
}

//获取性别
char get_sex(void)
{
    
	printf("请输入性别(w/m):");
	while(1)
	{
		int cmd = getch();
		if('M' == cmd || 'm' == cmd || 'w' == cmd || 'm' == cmd) 
    	{
			printf("%c\n",cmd);
			if('A' < cmd && 'Z' > cmd)
				cmd-=32;
			return cmd;
		}
	}
}

//获取字符串
char* get_str(char* str,size_t size)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;
	assert(NULL!=str && size>1);
	size_t len;
	while(1)
	{
		len = strlen(fgets(str,size,stdin));
		if(1==len)
		{
			printf("\n输入不能为空，请重新输入：");
		}
		else
		{
			break;
		}
	}
	if('\n' == str[len-1])
	{
		str[len-1] = '\0';
	}
	else
	{
		while('\n' != getchar());
	}
	return str;
}

//延时显示
void show_msg(const char* msg,float sec)
{
	printf("\033[01;32m%s\033[00m\n",msg);
	usleep(sec*1000000);
}

//按任意键继续
void anykey_continue(void)
{
    stdin->_IO_read_ptr = stdin->_IO_read_end;
	puts("\n按任意键继续...");
	getch();
}

//获取密码
char* get_passwd(char* passwd,size_t size)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;

	int i = 0;
	while(i < size-1)
	{
		passwd[i] = getch();
		// 读取到退格键
		if(127 == passwd[i])
		{
			// 数组中有已输入密码
			if(i > 0)
		    {
		    	// 删除一位密码
		    	i--;
		    	printf("\b \b");
	    	}
	    	continue;
	    }
		i++;
		printf("*");
	}
	passwd[size-1] = '\0';
	printf("\n");
	return passwd;
}

//得到数量
int file_num(char *NAME,char* name)
{
	char NAME_path[255];
	sprintf(NAME_path,"%s%s",NAME_path,name);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL == rfp)
		return 0;
	int num;
	fscanf(rfp,"%d",&num);
	fclose(rfp);
	return num;
}

//消除某一记录
bool del_record(char* NAME,char* name,char* record)
{
	char NAME_path[255];
	int num;
    sprintf(NAME_path,"%s%s",NAME,name);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL == rfp)
	{
        return false;
	}
	fscanf(rfp,"%d",&num);
	char arr[num][14];
	for(int i=0;i<num;i++)
	{
		fscanf(rfp,"%s",arr[i]);
		if(!strcmp(record,arr[i]))
		{	
			i--;
			num--;
		}
	}
	fclose(rfp);
	
	if(num == 0)
	{
		remove(NAME_path);
		return true;
	}
	FILE* wfp = fopen(NAME_path,"w");
	if(NULL == wfp)
	{
        return false;
	}
	fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s\n",arr[i]);
	}
	fclose(wfp);
	return true;
}

//添加一条记录
bool add_record(char* NAME,char* name,char* record)
{
	char NAME_path[255];
	int num=0;
    sprintf(NAME_path,"%s%s",NAME,name);
	FILE* rfp = fopen(NAME_path,"r");
	if(NULL != rfp)
	{
        fscanf(rfp,"%d",&num);
		
	}
	char arr[++num][14];
	if(NULL != rfp)
		for(int i=0;i<num;i++)
			fscanf(rfp,"%s",arr[i]);
	strcpy(arr[num-1],record);
	if(NULL != rfp)
		fclose(rfp);

	FILE* wfp = fopen(NAME_path,"w");
	if(NULL == wfp)
	{
        return false;
	}
	fprintf(wfp,"%d\n",num);
	for(int i=0;i<num;i++)
	{
		fprintf(wfp,"%s\n",arr[i]);
	}
	fclose(wfp);
	return true;
}

//删除文件
void del(char* NAME,char* name)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%s",NAME,name);
	remove(NAME_path);
}

//工号查找操作员
bool read_worker_id(int id,Worker* worker)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%d",WORKER_PATH,id);

    FILE* rfp = fopen(NAME_path,"r");
    if(NULL == rfp)
        return false;
    fscanf(rfp,"%s %s %s %c",
           worker->name,
           worker->password,
           worker->phone,
           &worker->sex);
    fclose(rfp);
    return true;   
}

//写入worker_id
bool write_worker_id(int id,Worker* worker)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%d",WORKER_PATH,id);
    FILE* wfp = fopen(NAME_path,"w");
    if(NULL == wfp)
        return false;
    fprintf(wfp,"%s %s %s %c\n",
           worker->name,
           worker->password,
           worker->phone,
           worker->sex);
    fclose(wfp);
    return true;   
}

//读出员工总数和下标 
bool read_worker_all(int id,All* all)
{
	
    FILE* rfp = fopen(WORKER_ALL_PATH,"r");
    if(NULL == rfp)
        return NULL;
    fscanf(rfp,"%d %d",
           &all->num,
           &all->cnt);
    fclose(rfp);
    return true;   
}

//删除一个工号
bool del_worker_id(int gid)
{
	FILE* rfp = fopen(WORKER_ALL_PATH,"r");
    if(NULL == rfp)
        return false;
	All all;
	fscanf(rfp,"%d %d",&all.num,&all.cnt);
	int id[all.num]; 	
	int id_cnt = -1;
	for(int i=0;i<all.num;i++)
	{
		fscanf(rfp,"%d",&id[i]);
		if(gid ==id[i])	
			id_cnt = i;
	}
	if(-1 == id_cnt)
		return false;
	id[id_cnt]=id[all.num-1];	
	all.num--;
	fclose(rfp);

	FILE* wfp = fopen(WORKER_ALL_PATH,"w");
    if(NULL == wfp)
        return false;
	fprintf(wfp,"%d %d\n",all.num,all.cnt);

	for(int i=0;i<all.num;i++)
		fprintf(wfp,"%d\n",id[i]);
	fclose(wfp);

	char NAME_path[255];
    sprintf(NAME_path,"%s%d",WORKER_PATH,gid);
	remove(NAME_path);
    return true;   		
}

//生成一个工号
int make_worker_id(void)
{
    FILE* rfp = fopen(WORKER_ALL_PATH,"r");
    if(NULL == rfp)
        return -2;
	All all;
	fscanf(rfp,"%d %d",&all.num,&all.cnt);
	all.num++;
	all.cnt++;
	int id[all.num]; 	
	for(int i=0;i<all.num-1;i++)
		fscanf(rfp,"%d",&id[i]);
	id[all.num-1] = all.cnt;
	fclose(rfp);
	

    FILE* wfp = fopen(WORKER_ALL_PATH,"w");
    if(NULL == wfp)
        return -1;
	fprintf(wfp,"%d %d\n",all.num,all.cnt);

	for(int i=0;i<all.num;i++)
		fprintf(wfp,"%d\n",id[i]);
	fclose(wfp);
    return all.cnt;   
}
//写入book_isbn
bool write_book_isbn(Book_TYPE* data)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%s",BOOK_ISBN_PATH,data->isbn);
 	FILE* wfp = fopen(NAME_path,"w");
    if(NULL == wfp)
        return false;
	fprintf(wfp,"%s %s %s %s %s %lf %u %u %u %u %u %d\n",
          data->name,
          data->isbn,
          data->press,
          data->author,
          data->position,
          data->price,
          data->purchase_num,
          data->now_num,
          data->stock_num,
          data->natural_damage,
          data->person_damage,
          data->off);
	fclose(wfp);
 	return true; 
}

//读出book_isbn
bool read_book_isbn(char* isbn,Book_TYPE* data)
{
	char NAME_path[255];
    sprintf(NAME_path,"%s%s",BOOK_ISBN_PATH,isbn);
 	FILE* rfp = fopen(NAME_path,"r");
    if(NULL == rfp)
        return false;
	fscanf(rfp,"%s %s %s %s %s %lf %u %u %u %u %u %d",
          data->name,
          data->isbn,
          data->press,
          data->author,
          data->position,
          &data->price,
          &data->purchase_num,
          &data->now_num,
          &data->stock_num,
          &data->natural_damage,
          &data->person_damage,
          &data->off);
    fclose(rfp);  
	return true; 
}
